<template>
  <div class="register-container">
    <!-- 动态背景 -->
    <div class="tech-background">
      <div class="lines"></div>
      <div class="glow"></div>
      <div class="particles"></div>
    </div>

    <div class="register-box">
      <h2>用户注册</h2>
      <el-form>
        <el-form-item>
          <el-input v-model="username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="password" type="password" placeholder="请输入密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="confirmPassword" type="password" placeholder="请确认密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-button type="primary" class="register-btn" @click="handleRegister">注册</el-button>
        <div class="login-link">
          已有账号？<router-link to="/login">去登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { register } from '@/api'

const router = useRouter()
const username = ref('')
const password = ref('')
const confirmPassword = ref('')

const handleRegister = async () => {
  if (!username.value || !password.value || !confirmPassword.value) {
    ElMessage.warning('请填写完整信息')
    return
  }

  if (password.value !== confirmPassword.value) {
    ElMessage.error('两次输入的密码不一致')
    return
  }

  try {
    console.log('正在尝试注册...')
    console.log('用户名:', username.value)
    
    const response = await register({
      username: username.value,
      password: password.value
    })
    
    console.log('注册响应:', response)

    // 检查响应中是否包含用户ID，如果有ID说明注册成功
    if (response && response.id) {
      ElMessage.success('注册成功')
      router.push('/login')
    } else if (response && response.message) {
      ElMessage.error(response.message)
    } else {
      ElMessage.error('注册失败，请重试')
    }
  } catch (error) {
    console.error('注册失败，详细错误:', error)
    if (error.response) {
      console.error('错误响应:', error.response)
      console.error('错误状态:', error.response.status)
      console.error('错误数据:', error.response.data)
      
      if (error.response.status === 409) {
        ElMessage.error('用户名已存在')
      } else if (error.response.status === 400) {
        ElMessage.error(error.response.data?.message || '注册信息不正确')
      } else if (error.response.status === 500) {
        ElMessage.error('服务器内部错误，请稍后重试')
      } else {
        ElMessage.error(error.response.data?.message || '注册失败，请重试')
      }
    } else if (error.request) {
      console.error('没有收到响应:', error.request)
      ElMessage.error('无法连接到服务器，请检查网络连接')
    } else {
      console.error('请求配置错误:', error.message)
      ElMessage.error('注册请求失败，请重试')
    }
  }
}
</script>

<style scoped>
.register-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #001529;
  position: relative;
  overflow: hidden;
}

/* 动态背景样式 */
.tech-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.lines {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    linear-gradient(90deg, rgba(0, 255, 255, 0.03) 1px, transparent 1px) 0 0,
    linear-gradient(0deg, rgba(0, 255, 255, 0.03) 1px, transparent 1px) 0 0;
  background-size: 50px 50px;
  animation: moveBg 20s linear infinite;
}

.glow {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, rgba(0, 255, 255, 0.1) 0%, transparent 70%);
  transform: translate(-50%, -50%);
  animation: pulse 4s ease-in-out infinite;
}

.particles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    radial-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px),
    radial-gradient(rgba(0, 255, 255, 0.15) 2px, transparent 2px);
  background-size: 40px 40px, 80px 80px;
  animation: float 20s linear infinite;
}

@keyframes moveBg {
  0% { transform: translate(0, 0); }
  100% { transform: translate(50px, 50px); }
}

@keyframes pulse {
  0% {
    opacity: 0.5;
    transform: translate(-50%, -50%) scale(0.8);
  }
  50% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0.5;
    transform: translate(-50%, -50%) scale(0.8);
  }
}

@keyframes float {
  0% { background-position: 0 0, 0 0; }
  100% { background-position: 40px 40px, 80px 80px; }
}

.register-box {
  position: relative;
  z-index: 1;
  width: 400px;
  padding: 40px;
  background: rgba(0, 21, 41, 0.8);
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 255, 255, 0.1);
}

.register-box::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg, 
    rgba(0, 255, 255, 0.2),
    rgba(0, 255, 255, 0),
    rgba(0, 255, 255, 0.2)
  );
  border-radius: 12px;
  z-index: -1;
  animation: borderGlow 4s linear infinite;
}

@keyframes borderGlow {
  0% { filter: hue-rotate(0deg); }
  100% { filter: hue-rotate(360deg); }
}

h2 {
  color: #00ffff;
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}

.el-input {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  background: rgba(0, 255, 255, 0.05);
  border: 1px solid rgba(0, 255, 255, 0.2);
  box-shadow: none;
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(0, 255, 255, 0.4);
}

:deep(.el-input__inner) {
  color: #fff;
}

:deep(.el-input__prefix-inner) {
  color: rgba(0, 255, 255, 0.6);
}

.register-btn {
  width: 100%;
  margin-top: 20px;
  background: linear-gradient(45deg, #00a8ff, #00fff2);
  border: none;
  height: 40px;
  font-size: 16px;
  transition: all 0.3s;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 255, 255, 0.3);
}

.login-link {
  text-align: center;
  margin-top: 15px;
  color: rgba(255, 255, 255, 0.7);
}

.login-link a {
  color: #00ffff;
  text-decoration: none;
  margin-left: 5px;
}

.login-link a:hover {
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5);
}
</style> 